import { _decorator, Component, EventTouch, Label, Node, ProgressBar, Sprite, SpriteFrame, Toggle, Tween, tween, UITransform, v2, v3, Vec3 } from 'cc';
import { BaseUI } from '../../framework/base/BaseUI';
import { ADType, BaseEventName } from '../../framework/config/Appcfg';
import { AudioMgr } from '../../framework/manager/AudioMgr';
import { SDKMgr } from '../../framework/manager/SDKMgr';
import { UIMgr } from '../../framework/manager/UIMgr';
import { AudioId, UIID } from '../config/Config';
import GameMgr from '../manager/GameMgr';
import { PlayerMgr } from '../manager/PlayerMgr';
import { GameData, starBoxCfg } from '../excel/GameData';
import { DataMgr } from '../manager/DataMgr';
import { LogMgr } from '../../framework/manager/LogMgr';
import { Utils } from '../../framework/utils/Utils';
import { TimeUtils } from '../../framework/utils/TimeUtils';
import { UserData } from '../datas/UserData';
const { ccclass, property } = _decorator;

@ccclass('UIStar')
export class UIStar extends BaseUI {



    /**关卡leb */
    @property(Label)
    topLeb: Label = null;
    /**选项1存在 */
    @property(Node)
    tag1Has: Node = null;
    /**选项1数量 */
    @property(Label)
    tog1Leb: Label = null;
    /**选项1不存在 */
    @property(Node)
    tag1UnHas: Node = null;

    /**选项2拥有 */
    @property(Node)
    tag2Has: Node = null;
    /**选项2数量 */
    @property(Label)
    tog2Leb: Label = null;
    /**选项2不存在 */
    @property(Node)
    tag2UnHas: Node = null;

    /**选项3拥有 */
    @property(Node)
    tag3Has: Node = null;
    /**选项3数量 */
    @property(Label)
    tog3Leb: Label = null;
    /**选项3不存在 */
    @property(Node)
    tag3UnHas: Node = null;

    //---------------------
    //技能提示
    @property(Node)
    skinTiipsN: Node = null
    //技能图片
    @property(Sprite)
    skinSpr: Sprite = null
    //技能描述
    @property(Label)
    skinDesc: Label = null
    //技能名字
    @property(Label)
    skinName: Label = null

    //技能图片
    @property(SpriteFrame)
    skinSprN: SpriteFrame[] = []


    //技能提示
    @property(Toggle)
    toggleSkin: Toggle[] = []



    /**广告按钮 */
    @property(Node)
    videoBtn: Node = null;
    //框
    @property(Node)
    dikuangN: Node = null



    //指引
    @property(Node)
    guildN: Node = null
    //手指
    @property(Node)
    fingerN: Node = null
    //技能描述
    @property(Label)
    guideTips: Label = null


    /**源节点数据 */
    _root: Node = null;
    /**是否是挑战 */
    private isChange: boolean = false

    public init(param1?: any, param2?: any, param3?: any, param4?: any, param5?: any): void {
        this.isChange = param1 ? true : false
    }

    start() {
        console.warn("进入界面")
        this.node.on(Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.on(Node.EventType.TOUCH_END, this.touchStartEvent, this);
        this.guildN.active = false
        GameMgr.getInstance().initGameSkin()
        if (!this.isChange) {
            if (PlayerMgr.getInstance().getUserData().getLevel() <= 11) {
                this.clickStar()
                return
            } else if (PlayerMgr.getInstance().getUserData().getLevel() > 11 && PlayerMgr.getInstance().getUserData().getLevel() <= 19) {
                this.videoBtn.active = false
                this.dikuangN.getComponent(UITransform).height = 848
            }
            // this.initData();
            this.initModule();
            this.guideState()
            if (PlayerMgr.getInstance().getUserData().getLevel() < 16) {
                this.toggleSkin[1].node.getChildByName('daojuk').active = this.toggleSkin[2].node.getChildByName('daojuk').active = false
                this.toggleSkin[1].node.getChildByName('clock').active = this.toggleSkin[2].node.getChildByName('clock').active = true
            } else if (PlayerMgr.getInstance().getUserData().getLevel() < 20) {
                this.toggleSkin[2].node.getChildByName('daojuk').active = false
                this.toggleSkin[2].node.getChildByName('clock').active = true
            }
        } else {

        }

    }

    closeGuide() {
        Tween.stopAllByTarget(this.guildN)
        this.guildN.active = false
    }
    touchStartEvent(event: EventTouch) {
        this.closeGuide()
    }
    guideState() {
        this.guildN.active = false
        switch (PlayerMgr.getInstance().getUserData().getLevel()) {
            case 12:
            case 16:
            case 20:
                this.guildN.active = true
                if (PlayerMgr.getInstance().getUserData().getSkillById(8) <= 0 && PlayerMgr.getInstance().getUserData().getLevel() == 12) {
                    PlayerMgr.getInstance().getUserData().addSkillNum(1, 8)
                }
                if (PlayerMgr.getInstance().getUserData().getSkillById(9) <= 0 && PlayerMgr.getInstance().getUserData().getLevel() == 16) {
                    PlayerMgr.getInstance().getUserData().addSkillNum(1, 9)
                }
                if (PlayerMgr.getInstance().getUserData().getSkillById(10) <= 0 && PlayerMgr.getInstance().getUserData().getLevel() == 20) {
                    PlayerMgr.getInstance().getUserData().addSkillNum(1, 10)
                }
                this.fingerN.setScale(0.8, 0.8, 1)
                let _nodeP2 = PlayerMgr.getInstance().getUserData().getLevel() == 12 ? new Vec3(197,95):
                    PlayerMgr.getInstance().getUserData().getLevel() == 16 ? new Vec3(197,-160) : new Vec3(197,-385)

                this.guideTips.string = PlayerMgr.getInstance().getUserData().getLevel() == 12 ? '这关太难了！使用魔法棒消除多组花朵' :
                    PlayerMgr.getInstance().getUserData().getLevel() == 16 ? '这关太难了！使用加时增加额外时长' : '使用道具获得双倍阳光奖励'

                this.fingerN.setPosition(_nodeP2.x, _nodeP2.y, 0)
                tween(this.fingerN)
                    .to(0.3, { position: new Vec3(_nodeP2.x-10 , _nodeP2.y + 10, 0) })
                    .to(0.3, { position: new Vec3(_nodeP2.x, _nodeP2.y , 0) })
                    .union()
                    .repeatForever()
                    .start();
                break;

            default:
                break;
        }
        this.setPropN()
    }

    clickClock() {
        UIMgr.getInstance().openUI(UIID.UITips, UIID.Default, '当前道具还未解锁');
    }

    protected onLoad(): void {

        this.skinTiipsN.active = false

        this.topLeb.string = this.isChange ?
            '第' + PlayerMgr.getInstance().getUserData().getChangeLv() + '关' :
            '第' + PlayerMgr.getInstance().getUserData().getLevel() + '关'
    }

    /**免费礼物 */
    clickFree() {
        SDKMgr.getInstance().playVideo((isComp) => {
            if (isComp) {
                if (this.node && this.node.isValid) {
                    GameMgr.getInstance().setGameSkin(0, 1)
                    GameMgr.getInstance().setGameSkin(1, 1)
                    GameMgr.getInstance().setGameSkin(2, 1)
                    console.warn('免费礼物');
                    this.openUICallBack(UIID.UILoad, () => {
                        if (UIMgr.getInstance().getUIById(UIID.UIGame)) {
                            UIMgr.getInstance().closeById(UIID.UIGame);
                        }
                        if (this.isChange) {
                            this.openUI(UIID.UIGame, PlayerMgr.getInstance().getUserData().getChangeLv(), null, null, true);
                        } else
                            this.openUI(UIID.UIGame, PlayerMgr.getInstance().getUserData().getLevel());
                    });
                    this.closeUI();
                }
            }
            SDKMgr.getInstance().reportEvent("showad", {
                "adtype": "video",
                "opportunity": '' + ADType.ALL,
                "video_complete": isComp + '',
                "is_stage": "false",
                "stage": '主页'
            })
        }, ADType.ALL)
    }

    getMoney() {
        if (PlayerMgr.getInstance().getUserData().getGoldBoxNum() < DataMgr.getInstance().getItemCfgById(11).initCount) {
            UIMgr.getInstance().openUI(UIID.UITips, UIID.Default, '金币不足再努努力吧');
            return
        }

    }
    initModule() {
        this._root = this.node.getChildByName("root");
    }

    initData() {
        // this.levelInfo = DataMgr.getInstance().getLevelInfo(this.curLevel);
    }

    initView() {

    }
    setPropN() {

        let skin8 = PlayerMgr.getInstance().getUserData().getSkillById(8)
        let skin9 = PlayerMgr.getInstance().getUserData().getSkillById(9)
        let skin10 = PlayerMgr.getInstance().getUserData().getSkillById(10)


        this.tag1Has.active = skin8 > 0
        this.tag2Has.active = skin9 > 0
        this.tag3Has.active = skin10 > 0
        
        this.tag1UnHas.active = skin8 == 0
        this.tag2UnHas.active = skin9 == 0
        this.tag3UnHas.active = skin10 == 0

        this.tog1Leb.string = skin8 + ''
        this.tog2Leb.string = skin9 + ''
        this.tog3Leb.string = skin10 + ''
    }
    private cSkinId: number = 0
    /**技能界面 */
    setSkinNode(skinId: number) {
        this.cSkinId = skinId
        this.skinTiipsN.active = true
        this.skinSpr.spriteFrame = this.skinSprN[skinId - 8]
        this.skinDesc.string = DataMgr.getInstance().getItemCfgById(skinId).propDec
        this.skinName.string = DataMgr.getInstance().getItemCfgById(skinId).propName
    }
    cliskVide() {
        SDKMgr.getInstance().playVideo((isComp) => {
            if (isComp) {
                if (this.node && this.node.isValid) {
                    PlayerMgr.getInstance().getUserData().addSkillNum(1, this.cSkinId)
                    console.warn('刷新道具' + this.cSkinId + '+1');
                    this.skinTiipsN.active = false
                    this.setPropN()
                }
            }
            SDKMgr.getInstance().reportEvent("showad", {
                "adtype": "video",
                "opportunity": (this.cSkinId == 8 ? ADType.MAGIC :
                    this.cSkinId == 9 ? ADType.ONTIME : ADType.DOUBLEXX) + '',
                "video_complete": isComp + '',
                "is_stage": "false",
                "stage": '主页'
            })
        }, this.cSkinId == 8 ? ADType.MAGIC :
            this.cSkinId == 9 ? ADType.ONTIME : ADType.DOUBLEXX)
    }
    clickClose() {
        UIMgr.getInstance().openUI(UIID.UIHome, UIID.Default);
        if (UIMgr.getInstance().getUIById(UIID.UIGame)) {
            UIMgr.getInstance().closeById(UIID.UIGame);
        }
        this.closeUI()
    }
    closeTips() {
        this.skinTiipsN.active = false
    }
    clickBuySk() {
        if (PlayerMgr.getInstance().getUserData().subGoldNum(200)) {
            PlayerMgr.getInstance().getUserData().addSkillNum(1, this.cSkinId)
            this.setPropN()
        } else {
            this.openUI(UIID.UIShop);
            // UIMgr.getInstance().openUI(UIID.UITips, UIID.Default, '金币不足~');
        }
    }
    clickStar() {
        if (this.isChange) {
            if (PlayerMgr.getInstance().getUserData().subRoleHpNum(1)) {
                UIMgr.getInstance().closeById(UIID.UIGame);
                let cfg = DataMgr.getInstance().getItemCfgById(1)
                PlayerMgr.getInstance().getUserData().setHpAddTime(cfg.timeAdd + TimeUtils.GetTimeBySecond())


                //审核模式 直接进入24关

                this.openUICallBack(UIID.UILoad, () => {
                    UIMgr.getInstance().closeById(UIID.UIHome);
                    this.openUI(UIID.UIGame, PlayerMgr.getInstance().getUserData().getChangeLv(), null, null, true);
                });
                this.closeUI();
            } else {
                this.openUI(UIID.UIBuyHp);
            }
        } else {
            //关闭游戏界面
            if (PlayerMgr.getInstance().getUserData().subRoleHpNum(1)) {
                UIMgr.getInstance().closeById(UIID.UIGame);
                let cfg = DataMgr.getInstance().getItemCfgById(1)
                PlayerMgr.getInstance().getUserData().setHpAddTime(cfg.timeAdd + TimeUtils.GetTimeBySecond())


                //审核模式 直接进入24关

                this.openUICallBack(UIID.UILoad, () => {
                    UIMgr.getInstance().closeById(UIID.UIHome);
                    this.openUI(UIID.UIGame, PlayerMgr.getInstance().getUserData().getLevel());
                });
                this.closeUI();
            } else {
                if (PlayerMgr.getInstance().getUserData().getLevel() <= 11) {
                    this.closeUI()
                }
                this.openUI(UIID.UIBuyHp);
            }
        }
    }

    clickSkin(node: EventTouch, args: number) {
        AudioMgr.getInstance().playEffect(AudioId.btnClick)
        this.closeGuide()
        switch (+args) {
            case 1:
                if (PlayerMgr.getInstance().getUserData().getSkillById(8) > 0) {
                    this.scheduleOnce(() => {
                        if (this.node && this.node.isValid)
                            GameMgr.getInstance().setGameSkin(0, this.toggleSkin[0].isChecked == true ? 1 : -1)
                    })
                } else {
                    this.setSkinNode(8)
                    this.scheduleOnce(() => {
                        if (this.node && this.node.isValid)
                            this.toggleSkin[0].isChecked = false
                    })
                }
                break;
            case 2:
                if (PlayerMgr.getInstance().getUserData().getSkillById(9) > 0) {
                    this.scheduleOnce(() => {
                        if (this.node && this.node.isValid)
                            GameMgr.getInstance().setGameSkin(1, this.toggleSkin[1].isChecked == true ? 1 : -1)
                    })
                } else {
                    this.setSkinNode(9)
                    this.scheduleOnce(() => {
                        if (this.node && this.node.isValid)
                            this.toggleSkin[1].isChecked = false
                    })

                }
                break;
            case 3:
                if (PlayerMgr.getInstance().getUserData().getSkillById(10) > 0) {
                    this.scheduleOnce(() => {
                        if (this.node && this.node.isValid)
                            GameMgr.getInstance().setGameSkin(2, this.toggleSkin[2].isChecked == true ? 1 : -1)
                    })

                } else {
                    this.setSkinNode(10)
                    this.scheduleOnce(() => {
                        if (this.node && this.node.isValid)
                            this.toggleSkin[2].isChecked = false
                    })
                }
                break;
            default:
                break;
        }
    }


    onCloseUI(uid: any, msg: string) {
        this.closeUI()
    }
}

